package com.example.markdownviewer;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import java.util.HashMap;
import java.util.Map;

public class PluginSettingsDialog extends DialogFragment {

    public interface PluginSettingsListener {
        void onPluginSettingsChanged();
    }

    static final String PREFS_NAME = "MarkwonPluginPrefs";
    static final String KEY_HTML = "plugin_html";
    static final String KEY_IMAGES = "plugin_images";
    static final String KEY_TABLES = "plugin_tables";
    static final String KEY_SYNTAX = "plugin_syntax";
    static final String KEY_INLINE = "plugin_inline";
    static final String KEY_LATEX = "plugin_latex";

    private PluginSettingsListener listener;
    private final Map<String, CheckBox> checkBoxMap = new HashMap<>();

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        try {
            listener = (PluginSettingsListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context + " must implement PluginSettingsListener");
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
        LayoutInflater inflater = requireActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_plugin_settings, null);

        // 初始化复选框
        checkBoxMap.put(KEY_HTML, view.findViewById(R.id.checkbox_html));
        checkBoxMap.put(KEY_IMAGES, view.findViewById(R.id.checkbox_images));
        checkBoxMap.put(KEY_TABLES, view.findViewById(R.id.checkbox_tables));
        checkBoxMap.put(KEY_SYNTAX, view.findViewById(R.id.checkbox_syntax));
        checkBoxMap.put(KEY_INLINE, view.findViewById(R.id.checkbox_inline));
        checkBoxMap.put(KEY_LATEX, view.findViewById(R.id.checkbox_latex));

        // 加载保存的设置
        loadSettings();

        // 设置按钮
        Button saveButton = view.findViewById(R.id.button_save);
        Button cancelButton = view.findViewById(R.id.button_cancel);

        saveButton.setOnClickListener(v -> {
            saveSettings();
            listener.onPluginSettingsChanged();
            dismiss();
        });

        cancelButton.setOnClickListener(v -> dismiss());

        builder.setView(view)
               .setTitle(R.string.plugin_settings);

        return builder.create();
    }

    private void loadSettings() {
        SharedPreferences prefs = requireActivity().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        
        // 默认所有插件都启用
        for (Map.Entry<String, CheckBox> entry : checkBoxMap.entrySet()) {
            boolean isEnabled = prefs.getBoolean(entry.getKey(), true);
            entry.getValue().setChecked(isEnabled);
        }
    }

    private void saveSettings() {
        SharedPreferences prefs = requireActivity().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        
        for (Map.Entry<String, CheckBox> entry : checkBoxMap.entrySet()) {
            editor.putBoolean(entry.getKey(), entry.getValue().isChecked());
        }
        
        editor.apply();
    }

    /**
     * 获取插件启用状态
     * @param context 上下文
     * @return 包含插件启用状态的Map
     */
    public static Map<String, Boolean> getPluginSettings(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        Map<String, Boolean> settings = new HashMap<>();
        
        settings.put(KEY_HTML, prefs.getBoolean(KEY_HTML, true));
        settings.put(KEY_IMAGES, prefs.getBoolean(KEY_IMAGES, true));
        settings.put(KEY_TABLES, prefs.getBoolean(KEY_TABLES, true));
        settings.put(KEY_SYNTAX, prefs.getBoolean(KEY_SYNTAX, true));
        settings.put(KEY_INLINE, prefs.getBoolean(KEY_INLINE, true));
        settings.put(KEY_LATEX, prefs.getBoolean(KEY_LATEX, true));
        
        return settings;
    }
}